(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.lookup.addFeeForReceiveController", addFeeForReceiveController);
	addFeeForReceiveController.$inject = [ "$scope", "$q", "$uibModalInstance", "ajaxServiceFactory", "dialogsModule", "gridService", "modalService", "layoutConfig", "validatorConfig", "authService", "$filter", "arithService" ];
	function addFeeForReceiveController($scope, $q, $uibModalInstance, ajaxServiceFactory, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, $filter, arithService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("addFeeForReceiveLookup"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getFeeCodeOptions : {
				url : "do/cls/settlement/feeCodeOptions",
				method : "post"
			},
			getFeeCcyOptions : {
				url : "do/cls/dataList/currencies",
				method : "get"
			},
			getAllocCodeOptions : {
				url : "do/cls/dataList/allocCodeList",
				method : "get"
			}
		});
		
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.settlement = ctrl.settlement || {};
		ctrl.ok = function() {
			var feeObj = ctrl.settlement.feeObj;
			var feeCode = feeObj.feeCode;
			var feeAmt = feeObj.feeAmt;
			var feeCcy = feeObj.feeCcy;
			if(feeCode != "" && (feeAmt-0) != 0 && feeCcy != ""){
				var param = new Object();
				param.itemType = "F";
				param.crdr = feeObj.crDrFlag;
				param.itemCcy = feeCcy;
				param.taxAmt = feeObj.feeTaxAmt ? feeObj.feeTaxAmt : 0;
				param.itemAmt = arithService.calc(feeAmt, param.taxAmt, "+");
				param.payCcy = feeCcy;
				param.payAmt = arithService.calc(feeAmt, param.taxAmt, "+");	
				
				param.allocCode = feeObj.allocCode;
				param.itemCode = feeCode;
				param.itemDesc = feeObj.feeDesc;
				param.itemSubtype = "";
						
				param.fxType = "C";
				param.fxRate = 1.0;
				param.fxType2 = "C";		
				param.fxRate2 = 1.0;		

				param.holdFlag = "N";
				param.waiveFlag = "N";
				param.uItemWaive = "Y";
				param.carryFlag = "N";
				param.transferFlag = "N";
				
				param.taxWaiveFlag = "N";
				param.childFlag = "N";
				param.feeNewFlag = "Y";
				
				param.feeId = "";				
				param.feeTaxFlag = feeObj.feeTaxFlag;
				param.parentItemSeq = 0;
				if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](param) !== false) {
					$uibModalInstance.close();
				}
			}else{
				if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"]() !== false) {
					$uibModalInstance.close();
				}
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.getFeeCodeOptions = function(feeObj) {
			var defer = $q.defer();
			ajaxService.getFeeCodeOptions(feeObj).then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		};
		
		ctrl.getFeeCcyOptions = function() {
			var defer = $q.defer();
			ajaxService.getFeeCcyOptions().then(function(response) {
				defer.resolve(response ? response : {});
			});
			return defer.promise;
		};
		
		ctrl.getAllocCodeOptions = function() {
			var defer = $q.defer();
			ajaxService.getAllocCodeOptions().then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		};
		
		ctrl.feeCodeChanged = function(){
			if(ctrl.settlement && ctrl.settlement.feeObj && ctrl.settlement.feeObj.feeCode){
				if($filter("encode")(ctrl.settlement.feeObj.feeCode_old) == $filter("encode")(ctrl.settlement.feeObj.feeCode)){
					return;
				}else{
					ctrl.settlement.feeObj.feeCode_old = ctrl.settlement.feeObj.feeCode;
				}
				if(ctrl.settlement.feeObj && !ctrl.settlement.feeObj.loanId){
					delete ctrl.settlement.feeObj.loanId;
				}
				authService.lookup(ctrl.settlement).then(function(response) {
					angular.merge(ctrl.settlement, response.data);
					setFeeCodeValue(ctrl.feeCodeOptions, ctrl.settlement.feeObj);
					ctrl.ccy = null;
					if(ctrl.settlement && ctrl.settlement.feeObj){
						ctrl.ccy = ctrl.settlement.feeObj.feeCcy;
					}
					if(ctrl.ccy == null){
						ctrl.ccy = "";
					}
				}, angular.noop);
			}
		};
		
		ctrl.init = function() {
			if(ctrl.dialogsModule.params.feeObj){
				ctrl.settlement.feeObj = ctrl.dialogsModule.params.feeObj;
			}
			ctrl.getFeeCcyOptions().then(function(response) {
				ctrl.feeCcyOptions = response ? response.data : [];
			}, angular.noop);
			ctrl.getAllocCodeOptions().then(function(response) {
				ctrl.allocCodeOptions = response;
			}, angular.noop);
			ctrl.getFeeCodeOptions(ctrl.settlement.feeObj).then(function(response) {
				ctrl.feeCodeOptions = response ? response : [];
			}, angular.noop);
	
		};
		
		function setFeeCodeValue(options, feeObj){
			for(var i = 0; i < options.length; i++){
				if($filter("encode")(options[i].value) == $filter("encode")(feeObj.feeCode)){
					feeObj.feeCode = options[i].value;
					break;
				}
			}
		}
	}
})(angular);